<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/1
 * Time: 16:03
 */
//php curl获得header检测GZip压缩的源代码
//方法一：使用get_headers()函数
function pre($data)
{
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

$url = "http://www.baidu.com/";
//pre(get_headers($url));
//输出
/*Array
(
    [0] => HTTP/1.1 200 OK
[1] => Date: Fri, 01 Sep 2017 08:09:51 GMT
[2] => Content-Type: text/html
[3] => Content-Length: 14613
    [4] => Last-Modified: Thu, 31 Aug 2017 03:03:00 GMT
[5] => Connection: Close
[6] => Vary: Accept-Encoding
[7] => Set-Cookie: BAIDUID=03B2FC5AAF997DB8048094875DD3FAAD:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[8] => Set-Cookie: BIDUPSID=03B2FC5AAF997DB8048094875DD3FAAD; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[9] => Set-Cookie: PSTM=1504253391; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[10] => P3P: CP=" OTI DSP COR IVA OUR IND COM "
[11] => Server: BWS/1.1
    [12] => X-UA-Compatible: IE=Edge,chrome=1
    [13] => Pragma: no-cache
[14] => Cache-control: no-cache
[15] => Accept-Ranges: bytes
)*/
//========================================================================================================================================
//方法二：使用http_response_header
$html = file_get_contents($url);
//pre($http_response_header); //和方法一的结果一致
//========================================================================================================================================
//方法三：使用stream_get_meta_data()函数
$fp = fopen($url, 'r');
//pre(stream_get_meta_data($fp));
//输出结果
/*Array
(
    [timed_out] =>
        [blocked] => 1
    [eof] =>
    [wrapper_data] => Array
(
    [0] => HTTP/1.1 200 OK
[1] => Date: Fri, 01 Sep 2017 08:14:24 GMT
[2] => Content-Type: text/html
[3] => Content-Length: 14613
            [4] => Last-Modified: Thu, 31 Aug 2017 03:03:00 GMT
[5] => Connection: Close
[6] => Vary: Accept-Encoding
[7] => Set-Cookie: BAIDUID=83347B356E5667B2D19AFE2A7E83A6E0:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[8] => Set-Cookie: BIDUPSID=83347B356E5667B2D19AFE2A7E83A6E0; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[9] => Set-Cookie: PSTM=1504253664; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
[10] => P3P: CP=" OTI DSP COR IVA OUR IND COM "
[11] => Server: BWS/1.1
            [12] => X-UA-Compatible: IE=Edge,chrome=1
            [13] => Pragma: no-cache
[14] => Cache-control: no-cache
[15] => Accept-Ranges: bytes
        )

    [wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => r
[unread_bytes] => 0
    [seekable] =>
    [uri] => http://www.baidu.com/
)*/
//========================================================================================================================================
//上述三种方法都可以轻松获得网页header信息，且包含的信息都已经相当丰富，满足一般要求，不过比较遗憾的是，上述三种方法都不能用来检测网页是否启用了GZip压缩。要检测GZip压缩，还需其他的方法才行。这里介绍的是用curl()函数来检测。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);  //输出header信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  //不显示网页内容
curl_setopt($ch, CURLOPT_ENCODING, ''); //允许执行gzip
$data = curl_exec($ch);
//pre(strlen($data));
//pre($data);
if (!curl_errno($ch)) {
    $info = curl_getinfo($ch);
//    Array
//    (
//        [url] => http://www.baidu.com/
//        [content_type] => text/html; charset=utf-8
//        [http_code] => 200
//        [header_size] => 1105
//        [request_size] => 84
//        [filetime] => -1
//        [ssl_verify_result] => 0
//        [redirect_count] => 0
//        [total_time] => 0.062
//        [namelookup_time] => 0
//        [connect_time] => 0.031
//        [pretransfer_time] => 0.031
//        [size_upload] => 0
//        [size_download] => 5341
//        [speed_download] => 86145
//        [speed_upload] => 0
//        [download_content_length] => -1
//        [upload_content_length] => -1
//        [starttransfer_time] => 0.062
//        [redirect_time] => 0
//        [redirect_url] =>
//        [primary_ip] => 61.135.169.125
//        [certinfo] => Array
//    (
//    )
//
//    [primary_port] => 80
//    [local_ip] => 192.168.101.236
//    [local_port] => 64171
//)
    $httpHeaderSize = $info['header_size'];  //header字符串体积
    $pHeader = substr($data, 0, $httpHeaderSize); //获得header字符串
//    pre($pHeader);
    $split = array("\r\n", "\n", "\r");////需要格式化header字符串
    $res_header=str_replace($split,'<br>',$pHeader);
    pre($pHeader);
   //结果
    /*HTTP/1.1 200 OK
    Date: Fri, 01 Sep 2017 08:30:24 GMT
    Content-Type: text/html; charset=utf-8
    Transfer-Encoding: chunked
    Connection: Keep-Alive
    Vary: Accept-Encoding
    Set-Cookie: BAIDUID=B5E4425018ABF5A10FAAEFA1CC551A60:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    Set-Cookie: BIDUPSID=B5E4425018ABF5A10FAAEFA1CC551A60; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    Set-Cookie: PSTM=1504254624; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    Set-Cookie: BDRCVFR[laz3IxV61qm]=mk3SLVN4HKm; path=/; domain=.baidu.com
    Set-Cookie: BDSVRTM=9; path=/
    Set-Cookie: BD_HOME=0; path=/
    Set-Cookie: H_PS_PSSID=; path=/; domain=.baidu.com
    P3P: CP=" OTI DSP COR IVA OUR IND COM "
    P3P: CP=" OTI DSP COR IVA OUR IND COM "
    Cache-Control: private
    Cxy_all: 47018152_dg+d6bb4cc1b047b7d8d416fbcc3f29d567
    Expires: Fri, 01 Sep 2017 08:30:24 GMT
    X-Powered-By: HPHP
    Server: BWS/1.1
    X-UA-Compatible: IE=Edge,chrome=1
    BDPAGETYPE: 1
    BDQID: 0x9ad02e9b00016657
    BDUSERID: 0
    Content-Encoding: gzip*/

}




